﻿using System;
using System.Collections.Generic;
using System.Text;
using IDAL;
using System.IO;


namespace BLL
{
    public class DatabaseInfo
    {
        public IDatabaseInfo myData;
        

        public DatabaseInfo(){
            myData = new DALFactory.DatabaseInfoFactory().CreateDatabaseInfo();
        }

        public bool TestServer()
        {
            return myData.ConnServer();
        }

        private string ConvertDataType(string dt)
        {
            if (dt.Equals("text") || dt.Equals("varchar") || dt.Equals("char"))
                return "string";
            //if (dt.Equals("int"))
            //    return "int";
            if (dt.Equals("bigint"))
                return "Int64";
            if (dt.Equals("smallint"))
                return "Int16";
            if (dt.Equals("tinyint"))
                return "byte";
            if (dt.Equals("bigint"))
                return "long";      
            if (dt.Equals("bit"))
                return "bool";
            if (dt.Equals("money") || dt.Equals("smallmoney") || dt.Equals("numeric"))
                return "decimal";
            if (dt.Equals("datetime") || dt.Equals("smalldatetime") || dt.Equals("timestamp"))
                return "DateTime";
            if (dt.Equals("real"))
                return "Single";
            if (dt.Equals("float"))
                return "double";
            if (dt.Equals("image") || dt.Equals("binary") || dt.Equals("varbinary"))
                return "byte[]";
            if (dt.Equals("uniqueidentifier"))
                return "Guid";
            return dt;
        }

        public bool MakeModel(string NameSpace, string ModelName, string Path)
        {
            FirstWordUpper fir = new FirstWordUpper();

            if (!Directory.Exists(Path))
                Directory.CreateDirectory(Path);

            int pathLength = Path.Length;
            if (Path.Substring(pathLength - 1, 1) != "\\")
            {
                Path += "\\";
            }

            FileStream fsOne = new FileStream(".\\ModelTemp\\" + ModelName + "\\content.txt", FileMode.Open);
            StreamReader readContent = new StreamReader(fsOne);
            string read = readContent.ReadToEnd();
            readContent.Close();
            fsOne.Close();

            FileStream fsThree = new FileStream(".\\ModelTemp\\" + ModelName + "\\element.txt", FileMode.Open);
            StreamReader readElement = new StreamReader(fsThree);
            string _read = readElement.ReadToEnd();
            readElement.Close();
            fsThree.Close();


            List<Model.TableModel> tl = myData.GetTableList();

            for (int i = 0; i < tl.Count; i++)
            {
                string writeContent = read;//将content中的内容赋值给writeContent
                string valueString = "";
                //string checkClassName = fir.AllName(tl[i].TableName);
                string checkClassName = tl[i].TableName;

                if (checkClassName.Trim().IndexOf(" ") > 0)
                {
                    checkClassName = checkClassName.Replace(' ', '_');
                }

                FileStream fsTwo = new FileStream(@"" + Path + "" + checkClassName + ".cs", FileMode.Create);
                StreamWriter write = new StreamWriter(fsTwo);
                writeContent = writeContent.Replace("$namespace$", fir.AllName(NameSpace));

                writeContent = writeContent.Replace("$classname$", checkClassName);

                List<Model.FieldModel> fl = myData.GetFieldList(tl[i].TableName);
                
                foreach (Model.FieldModel fm in fl)
                {
                    string afreshRead = _read;//将element中的内容赋值给afreshRead
                    afreshRead = afreshRead.Replace("$type$", ConvertDataType(fm.TypeName));// + (fm.CanEmpty ? "?" : string.Empty)
                    afreshRead = afreshRead.Replace("$name$", '_' + fm.FieldName);
                    afreshRead = afreshRead.Replace("$UpperName$", fm.FieldName);  //fir.AllName(fm.FieldName)
                    afreshRead = afreshRead.Replace("$Description$", fir.AllName(fm.Description));
                    valueString += afreshRead;
                }
                writeContent = writeContent.Replace("$content$", valueString);
                write.Write(writeContent);
                write.Flush();
                write.Close();
                fsTwo.Close();
            }

            return true;
        }
    }
}
